
from openpyxl import load_workbook
from openpyxl.styles import Font
from rich import print


def info(*args):
    print('[bold blue]信息[/bold blue]', *args)


def success(*args):
    print('[bold green]成功[/bold green]', *args)


def warning(*args):
    print('[bold yellow]注意[/bold yellow]', *args)


def print_map(map, name=''):
    print(name)
    for (k, v) in map.items():
        print(f'''    {k}: {v}''')


def warning_decrese(coordinate, old_val, new_val):
    try:
        if int(new_val) < int(old_val):
            warning(
                f'[bold yellow]{coordinate} 值变小 {old_val} -> {new_val}[/bold yellow]')
    except TypeError:
        pass
    except ValueError:
        pass
    finally:
        pass


def wan_to_yi(val):
    return round(int(val) / 10000, 2)



def get_font(color, size):
    return Font(u'宋体', size=size, bold=False,
                italic=False, strike=False, color=color)


def read_from_xlsx(path, row_offset=0, max_col=0):
    '''从 xlsx 文件读取数据，0索引'''
    wb = load_workbook(path, data_only=True)
    ws = wb.active
    data = list(ws.values)[row_offset:]
    res = []
    for row in data:
        sub_row = row if max_col == 0 else row[:max_col]
        res.append(sub_row)
    return res


def find_row(source_data, current_row, primary_col):
    matched = None
    for row in source_data:
        if row[primary_col] == current_row[primary_col].value:
            matched = row
            break
    return matched